//
//  MyCollectViewController.m
//  UIA段项目 墨家天气
//
//  Created by dllo on 15/9/2.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "MyCollectViewController.h"
#import "NovelDataBase.h"
#import "CollectModel.h"
#import "MyCollectCell.h"
#import "UIImageView+WebCache.h"
#import "DetailViewController.h"
#import "DetailDetai.h"
@interface MyCollectViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,retain)UIView *headerView;
@property (nonatomic,retain)NSMutableArray *arr;
@property (nonatomic,retain)UITableView *tableView;
@end

@implementation MyCollectViewController
-(void)dealloc
{
    [_headerView release];
    [_arr release];
    [_tableView release];
    
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    
    
    
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.arr = [NSMutableArray array];
        
      
    }
    
    
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.arr =  (NSMutableArray *)[NovelDataBase selectCollectAll];

    [self addTableView];
    [self addHeaderView];

}
- (void)addHeaderView
{
    
    self.headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 60  *OffHeight)];
    _headerView.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.3];
    [self.view addSubview:_headerView];
    [_headerView release];
    
    UIButton *back = [UIButton buttonWithType:UIButtonTypeSystem];
    back.frame = CGRectMake(5 * OffWidth, 15 * OffHeight, 50 * OffWidth, 50 * OffHeight);
    
    [back setTitle:@"返回" forState:UIControlStateNormal];
    [back setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [back addTarget:self action:@selector(gotoCollect:) forControlEvents:UIControlEventTouchUpInside];
    [self.headerView addSubview:back];
    
    UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake((self.view.frame.size.width / 2) - 50 * OffWidth, 20 * OffHeight, 100 * OffWidth, 30 * OffHeight)];
    title.textColor = [UIColor whiteColor];
    title.text = @"我的收藏";
    title.textAlignment = 1;
    [self.headerView addSubview:title];
    [title release];
    
}
- (void)addTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 100 * OffHeight;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    UIImageView *imageV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"shui.jpg"]];
    [self.tableView setBackgroundView:imageV];
    UIBlurEffect *ef = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *effV = [[UIVisualEffectView alloc]initWithEffect:ef];
    effV.frame = [UIScreen mainScreen].bounds;
    [imageV addSubview:effV];
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 60 * OffHeight)];
    self.tableView.tableHeaderView = view;
    
    
    
    
    [self.view addSubview:self.tableView];
    [_tableView release];
    
        
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    MyCollectCell *cell = [tableView dequeueReusableCellWithIdentifier:@"collet"];
    if (cell == nil) {
        cell = [[MyCollectCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"collet"];
        
    }
    
    CollectModel *model = [[CollectModel alloc]init];
    model  = [self.arr objectAtIndex:indexPath.row];
    

    NSURL *url = [NSURL URLWithString:model.imageStr];
    [cell.imamgeV sd_setImageWithURL:url];
    cell.titleStr.text = model.title;
    cell.backgroundColor = [UIColor clearColor];
    

    return cell;
    
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    
    DetailDetai *detai = [[DetailDetai alloc] init];
    NSMutableArray *arrya = [NSMutableArray array];
    for(CollectModel *model in self.arr) {

        
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        [dic setObject:model.imageStr forKey:@"image"];
        [dic setObject:model.title forKey:@"title"];
        [arrya addObject:dic];
        
    }
  
    detai.array = arrya.mutableCopy;
    detai.row  = indexPath.row;
    [self presentViewController:detai animated:YES completion:nil];

}
- (void)gotoCollect:(UIButton *)button
{
   

    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
- (void)viewWillAppear:(BOOL)animated
{
    
    self.arr =  (NSMutableArray *)[NovelDataBase selectCollectAll];
    [self.tableView reloadData];
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
